package org.example.everyday;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/6/14 11:36
 */
public class Lc498 {
    public static void main(String[] args) {
        Lc498 lc498 = new Lc498();
        int[][] nums = new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        int[] res = lc498.findDiagonalOrder(nums);
        System.out.println(res);

    }

    public int[] findDiagonalOrder(int[][] mat) {
        int m = mat.length;
        int n = mat[0].length;
        int[] res = new int[m * n];
        int pos = 0;
        for (int i = 0; i < m + n - 1; i++) {
            if(i % 2 == 1){
                int x = i < n ? 0 : i - n + 1;
                int y = i < n ? i : n - 1;
                while (x < m && y >= 0){
                    res[pos] = mat[x][y];
                    pos++;
                    x++;
                    y--;
                }
            }else{
                int x = i < m ? i : m - 1;
                int y = i < m ? 0 : i - m + 1;
                while (x >= 0 && y < n){
                    res[pos] = mat[x][y];
                    pos++;
                    x--;
                    y++;
                }
            }
        }
        return res;
    }

}
